<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: epost.php 6 2005-07-11 23:14:47Z say $
 */
 
include_once("../db.php");
include("auth.php");
include_once("top.php");
include_once("rollerinclude.php");

//set this to true if you want to disable mailsending
$debug=false;

if ($_REQUEST['mailprocess'] == 'true') {
	ini_set("SMTP", "mail.swiboda.com");
	ini_set("sendmail_from", $_REQUEST['from']);

	$subject = $_REQUEST['subject'];
	$body = $_REQUEST['body'];

	//build an array of recipients.
	$rcpt=array();
		
	if ($_REQUEST['coole'] == 'true')
		$rcpt=addThem($rcpt, "1");

	if ($_REQUEST['teite'] == 'true')
		$rcpt=addThem($rcpt, "-1");
		
	if ($_REQUEST['limbo'] == 'true')
		$rcpt=addThem($rcpt, "0");
		
	if (sizeof($_REQUEST['additional'])) {
		foreach ($_REQUEST['additional'] as $id) {
			$sql = "SELECT ID, Epost FROM deltakere WHERE ID=$id";
			$deltakerne = mysql_query($sql);
			if (mysql_num_rows($deltakerne)) {
				$delt=mysql_fetch_assoc($deltakerne);

				if (!in_array($delt['Epost'], $rcpt))
					$rcpt[$delt['ID']]=$delt['Epost'];
			} else
				$error[]="Feil ID";
		}
	}

	$errors=massMail($rcpt, $subject, $body);

	if (sizeof($errors)) {
		echo "\n<ul>\n";
		foreach ($errors as $error)
			echo "\t<li>$error</li>\n";
		echo "</ul>\n";
	}


	//add to archive
	$sql="INSERT INTO Eposter (Subject, Body, LaivID, Tidspunkt) VALUES ('$subject', '$body', $laivID, NOW())";

	mysql_query($sql) or die(mysql_error());
	$newid=mysql_insert_id();

	if ($debug) echo "<p>Added to db as ID $newid</p>";

	if ($newid) {
		foreach ($rcpt as $id=>$epost) {
			$sql = "INSERT INTO Epostmottakere (Epost, Deltaker) VALUES ($newid, $id)";
			mysql_query($sql);
		}
	}
}
?>
<h1>E-post til deltakere</h1>
<form method="post" action="epost.php">
<input type="hidden" name="mailprocess" value="true"/>

<table>
	<tr>
		<th>Send til:</th>
		<th>Og/eller:</th>
	</tr>
	<tr>
		<td>
			<p><input type="checkbox" name="coole" value="true"/>
			Alle godkjente<br/>
			
			<input type="checkbox" name="teite" value="true"/>
			Alle avsl�tte<br/>
			
			<input type="checkbox" name="limbo" value="true"/>
			Alle ikke behandlede<br/></p>
		</td>
		<td>
			<p>Du kan merke av flere.</p>
			<select name="additional[]" multiple="multiple">
			<?
			$deltakere=getDeltakerArray(true);
			foreach ($deltakere as $id=>$navn)
				echo "<option value=\"$id\">$navn</option>\n";
			?>
			</select>
		</td>
	</tr>
</table>
<p>
Tittel p� e-post:<br/>
<input name="subject" size="40"/>
</p>
<p>
Fra-adresse (m� v�re @wolfraidah.no, urv.no eller andre swiboda):<br/>
<input name="from" value="urv@urv.no"/>
</p>
<p>
Innhold:<br/>
<textarea name="body" cols="40" rows="10"></textarea>
</p>
<p>
<input type="submit" value="Send"/>
</p>
</form>

<p>Skjemaet vil bare sende samme mailen en gang til hver adresse.</p>



<?
if ($debug) {
	echo "<p>Scriptet er i DEBUG-modus: det vil gj�re alle steg unntatt � sende selve mailen.</p>\n";
}
?>
<h2>E-postarkiv</h2>
<table class="gridded">
<tr>
	<th>Tidspunkt</th>
	<th>Tittel</th>
	<th>Vis...</th>
</tr>
<?
$sql = "SELECT ID, DATE_FORMAT(Tidspunkt, '%d.%m.%Y %H:%i'), Subject FROM Eposter WHERE LaivID=$laivid";
$eposter = mysql_query($sql);

while ($epost = mysql_fetch_row($eposter)) {
	?>
	<tr>
		<td><?=$epost[1]?></td>
		<td><?=$epost[2]?></td>
		<td><a href="epost.php?vis=<?=$epost[0]?>">[vis]</a></td>
	</tr>
	<?
}
echo "</table>\n";


if (!empty($_REQUEST['vis'])) {
	$visid=mysql_escape_string($_REQUEST['vis']);

	echo "<h2>Vis e-post</h2>\n";

	$sql="SELECT *, DATE_FORMAT(Tidspunkt, '%d.%m.%Y %H:%i') AS Tid FROM Eposter WHERE ID=$visid";
	$eposten=mysql_query($sql) or die(mysql_error());

	if(mysql_num_rows($eposten)) {
		$epost=mysql_fetch_assoc($eposten);
		?>
		<table class="gridded">
		<tr>
		<th>Tidspunkt:</th>
		<td><?=$epost['Tid']?></td>
		</tr>
		<tr>
		<th>Emne:</th>
		<td><?=$epost['Subject']?></td>
		</tr>
		<tr>
		<th>Mottakere:</th>
		<td><ul>
		<?
		$sql="SELECT Navn, deltakere.Epost FROM Epostmottakere LEFT JOIN deltakere ON Epostmottakere.Deltaker=deltakere.ID WHERE Epostmottakere.Epost=$visid";
		$mottakere = mysql_query($sql);
		while ($mottaker = mysql_fetch_assoc($mottakere))
			echo "<li>{$mottaker['Navn']} &lt;{$mottaker['Epost']}&gt;</li>\n";
		?>
		</ul></td>
		</tr>
		<tr>
		<th>Innhold:</th>
		<td><?=nl2br($epost['Body'])?></td>
		</tr>
		</table>
		<?
	}
}

function oneMail($to, $subject, $body) {
	global $debug;

	if ($debug) {
		echo "DEBUG mode, not sending mail. To: $to<br/>Subject: $subject<br/>Body: ".nl2br($body)."<br/>\n";
		return true;
	} else {
		$retur = mail($to, $subject, $body);
		return $retur;
	}
}

function massMail($arrayTo, $subject, $body) {
	$completed=0;
	foreach ($arrayTo as $to) {
		if (oneMail($to, $subject, $body))
			$completed++;
		else
			$error[]="Couldn't send to $to.";
	}
	$error[]="Completed $completed mails without error.";

	return $error;
}

function addThem($array, $criterium) {
	global $laivid;

	$sql="SELECT ID, Epost FROM deltakere WHERE Godkjent=$criterium AND LaivID=$laivid";
	$alle=mysql_query($sql);
	while ($all=mysql_fetch_assoc($alle)) {
		if (!in_array($all['Epost'], $array)) 
			$array[$all['ID']]=$all['Epost'];
	}
	
	return $array;
}

include("bottom.php");
?>
